package com.example.batchprocessing;

import java.util.Date;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.launch.JobInstanceAlreadyExistsException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@Configurable
@EnableBatchProcessing
public class JobStart implements ApplicationContextAware {

  ApplicationContext applicationContext;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }

  @Autowired
  private JobLauncher jobLauncher;
  @Autowired
  private JobOperator jobOperator;

  @PostConstruct
  public void startLocalPartJob() throws Exception {
    log.info("启动 startLocalPartJob");
    Job job = (Job) applicationContext.getBean("partitionJdbcJob2");
    JobParameters parameters = new JobParametersBuilder()
        .addString("bizDate", "123", false)//添加参数
        .toJobParameters();
    jobLauncher.run(job, parameters);//启动任务
  }
}
